﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Chapter07
{
    public class Code02
    {
        private static string[] eTypes = { "none", "simple", "index", "nested", "nested index" };

        public static void Begin()
        {
            {
                int[] myArray = { 1, 2, 3, 4 };
                //int myElem = myArray[4];
            }
            {
                try
                {

                }
                catch 
                {
                    return;
                }
                finally
                {

                }
            }
        }

        public static void BeginEx02()
        {
            foreach (string eTpye in eTypes)
            {
                try
                {
                    Console.WriteLine("Main() try block readched.");
                    Console.WriteLine("ThrowException(\"{0}\") called.", eTpye);
                    ThrowException(eTpye);
                    Console.WriteLine("Main() try block continues.");
                }
                catch (IndexOutOfRangeException e)
                {
                    Console.WriteLine("Main() System.IndexOutOfRangeExcetion cath block reached. message:\n\"{0}\"", e.Message);
                }
                catch
                {
                    Console.WriteLine("Main() general catch block reached.");
                }
                finally
                {
                    Console.WriteLine("Mian() finally  block reached.");
                }
                Console.WriteLine();
            }
            Console.ReadLine();
        }

        private static void ThrowException(string exceptoinType)
        {
            Console.WriteLine("ThrowException(\"{0}\") reached.", exceptoinType);
            switch (exceptoinType)
            {
                case "none":
                    Console.WriteLine("Not throwing an exception.");
                    break;
                case "simple":
                    Console.WriteLine("Throwing System.Excetion.");
                    throw new Exception();
                case "index":
                    Console.WriteLine("Throwing System.IndexOutOfRangeExcetion.");
                    eTypes[4] = "error";
                    break;
                case "nested index":
                    try
                    {
                        Console.WriteLine("ThrowExcetion(\"nested index\") try block readched.");
                        Console.WriteLine("ThrowExcetion(\"index\") called.");
                        ThrowException("index");
                    }
                    catch
                    {
                        Console.WriteLine("ThrowExcetion(\"nested index\") general catch block readched.");
                        throw;
                    }
                    finally
                    {
                        Console.WriteLine("ThrowExcetion(\"nested index\") finally block readched.");
                    }
                    break;
            }
        }
    }
}
